#ifndef __IMAGE_BMP_H
#define __IMAGE_BMP_H

#include <config.h>

#ifdef BMP_SUPPORT
#include <gapi.h>
#include <types.h>

#define ERR_BMP_OPEN      -1
#define ERR_BMP_FORMAT    -2
#define ERR_BMP_ALLOC_MEM -3
#define ERR_BMP_FB_ADDR   -4

typedef struct {
   unsigned int size;               /* Header size in bytes      */
   int width,height;                /* Width and height of image */
   unsigned short int planes;       /* Number of colour planes   */
   unsigned short int bits;         /* Bits per pixel            */
   unsigned int compression;        /* Compression type          */
   unsigned int imagesize;          /* Image size in bytes       */
   int xresolution,yresolution;     /* Pixels per meter          */
   unsigned int ncolours;           /* Number of colours         */
   unsigned int importantcolours;   /* Important colours         */
} __attribute__ ((packed)) BMP_INFOHEADER;

typedef struct {
   unsigned short int type;                 /* Magic identifier            */
   unsigned int size;                       /* File size in bytes          */
   unsigned short int reserved1, reserved2;
   unsigned int offset;                     /* Offset to image data, bytes */
   BMP_INFOHEADER info;
} __attribute__ ((packed)) BMP_HEADER;

typedef struct {            /* the structure for a bitmap. */
  u16 biWidth;
  u16 biHeight;
  u16 biBitCount;
  u8  *bicolor_palatte;
  u8  *data;
  BOOLEAN direct_write_to_fb;
} BMP;

BMP *load_bmp(u8 *file);
int save_bmp(IMAGE *img, const char *fname);

#ifdef NON_OS
typedef FILE file_t;
FILE *bmp_fopen(const char *file, const char *mode) __attribute__((alias("fopen_nos")));
int bmp_fclose(FILE *file) __attribute__((alias("fclose_nos")));
u8 bmp_fgetc(FILE *file) __attribute__((alias("fgetc_nos")));
int bmp_fread(void *dst, u32 len, u32 num, FILE *file) __attribute__((alias("fread_nos")));
int bmp_fwrite(void *src, u32 len, u32 num, FILE *file) __attribute__((alias("fwrite_nos")));
#else
FILE *bmp_fopen(const char *file, const char *mode) __attribute__((alias("fopen")));
int bmp_fclose(FILE *file) __attribute__((alias("fclose")));
int bmp_fgetc(FILE *file) __attribute__((alias("fgetc")));
int bmp_fread(void *dst, u32 len, u32 num, FILE *file) __attribute__((alias("fread")));
int bmp_fwrite(void *src, u32 len, u32 num, FILE *file) __attribute__((alias("fwrite")));
#endif

#endif /* BMP_SUPPORT */
#endif /* __IMAGE_BMP_H */
